<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Calendar Class
 * File: calendar_class.php
 * Purpose: Generating calendars
 * Todo: Add improved functionality
 *
 */

class Calendar {
	public $calendar_dates;
	private $calendar_array = array();
	public $calendar_text;
	
	/**
	* Generates a calendar
	*
	* @return array or boolean on fail
	* @access public
	*/
	public function generateCalendar() {
		// make sure the dates are set
		if(isset($this->calendar_dates['month'], $this->calendar_dates['year'])) {
			// are set
			// amount of rows
			$rows = 1;
			
			// set calendar date data
			$date = mktime(12, 0, 0, $this->calendar_dates['month'], 1, $this->calendar_dates['year']);
			$dim = date('t', $date);
			$offset = date('w', $date);
			
			// update calendar_text with data
			$this->calendar_text = date('F Y', $date);
			
			// start the array
			$this->calendar_array[] = array('start' => true);
			
			// loop through "blank days"
			for($i = 1; $i <= $offset; $i++) {
				$this->calendar_array[] = array('blank_day' => true);
			}
			
			// loop through actual days
			for($day = 1; $day <= $dim; $day++) {
				// do we need a new line?
				if(($day + $offset - 1) % 7 == 0 && $day != 1) {
					// update the array
					$this->calendar_array[] = array('new_line' => true);
					
					// increment rows
					$rows++;
				}
				
				// set this as a day in the array
				$this->calendar_array[] = array('day' => $day);
			}
			
			// any more blanks day after actual days?
			while(($day + $offset) <= $rows * 7) {
				// is a blank day
				$this->calendar_array[] = array('blank_day' => true);
				
				// increment the day
				$day++;
			}
			
			// end this table
			$this->calendar_array[] = array('end' => true);
			
			// pass this array back to be parsed
			return $this->calendar_array;
		} else {
			// failed
			return false;
		}
	}
}

?>